﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;

namespace Codes {
    public class Message {

        public static bool add(string content, string UserName, string SourceUrl, string SourceParameter, string SourceTag, string State="1", string UserId="-1") {
            string sql="insert into [Message]([UserId],[Content],[AddTime],[UserName],[State],[SourceUrl],[SourceParameter],[SourceTag]) values(";
            sql+="@UserId,@Content,now(),@UserName,@State,@SourceUrl,@SourceParameter,@SourceTag)";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@UserId", UserId),
                new OleDbParameter("@Content", content),
                new OleDbParameter("@UserName", UserName),
                new OleDbParameter("@State", State),
                new OleDbParameter("@SourceUrl", SourceUrl),
                new OleDbParameter("@SourceParameter", SourceParameter),
                new OleDbParameter("@SourceTag", SourceTag),
            })>0;
        }

        public static bool reply(string ReplyContent, int ID) {
            string sql="update [Message] set [ReplyContent]=@ReplyContent,[ReplyAddTime]=now() where [ID]=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@ReplyContent", ReplyContent),
                new OleDbParameter("@ID", ID)
            })>0;
        }

        public static bool Manager(string content, int ID) {
            string sql="update [Message] set [Content]=@Content where [ID]=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@Content", content),
                new OleDbParameter("@ID", ID)
            })>0;
        }

        public static bool MassState(int state, string idList) {
            string sql="update [Message] set [State]=@State where [ID] in("+idList+")";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@State", state)
            })>0;
        }

        public static bool deleteMass(string idList) {
            string sql="delete from [Message] where ID in("+idList+")";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql)>0;
        }

        public static int getListCount(string key) {
            string sql ="select count(1) from [Message] ";
            if(!string.IsNullOrEmpty(key)) {
                sql+=" where SourceTag='"+key+"'";
            }
            return (int)Codes.AccessHelper.ExecuteScalar(connectionString.Content, sql);
        }
        public static System.Data.DataTable getList(string key, int pageindex, int pagesize) {
            int notTop=((pageindex-1)*pagesize);
            string sql=string.Empty, whereSql=string.Empty;
            if(!string.IsNullOrEmpty(key)) {
                whereSql+=" where SourceTag='"+key+"'";
            }
            if(notTop==0) {
                sql="select top "+pagesize+" * from [Message] "+whereSql+" order by [ID] asc";
            } else {
                sql ="select top "+pagesize+" * from [Message] where id not in(select top "+((pageindex-1)*pagesize)+" id from [Message] "+whereSql+") order by [ID] asc";
            }
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Content, sql).Tables[0];
        }

        public static System.Data.DataTable getInfo(string id) {
            string sql="select * from [Message] where id="+id;
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Content, sql).Tables[0];
        }
    }
}